﻿Imports System.IO
Imports System.Xml.Serialization
Imports System.Drawing
Public Class RCMS_Preferences_Serial
#Region "Private declarations"
    ' Mirror variables in RCMS_Preferences
    Private d As New RCMS_Preferences
    Public v_g_clds As String
    Public v_b_wp As String
    Public v_l_wp As String
    Public v_c_wp As String
    Public v_f_wp As Integer
    Public v_b_zone As String
    Public v_l_zone As String
    Public v_c_zone As String
    Public v_f_zone As Integer
    Public v_t_zone As String
    Public v_l_grid As String
    Public v_w_grid As Double
    Public v_h_grid As Double
    Public v_c_grid As Double
    Public v_m_grid As Double
    Public v_v_grid As Double
    Public v_t_grid As Double
    Public v_x_timo As Integer
    Public v_t_wpid As String
    Public v_t_flor As String
    Public v_t_room As String
    Public v_t_fpno As String
    Public v_t_fpid As String
    Public v_t_cbid As String
    Public v_x_cbno As Integer
    Public v_p_excl As String
    Public v_c_texc As String
    Public v_c_hexc As String
#End Region
#Region "Formatting functions"
    Private Function C2S(ByVal c As Color) As String
        Dim s As String
        s = Trim(Str$(c.R)) & "," & Trim(Str$(c.G)) & "," & Trim(Str$(c.B))
        Return s
    End Function
    Private Function SC(ByVal s As String, ByVal v As Color) As Color
        Dim x As String = s
        If x Is Nothing OrElse x = "" Then
            Return v
        End If
        Dim c As Color
        Dim r As Integer = 0, g As Integer = 0, b As Integer = 0
        Dim a As Object = Split(x, ",")
        r = Val(a(0)) : g = Val(a(1)) : b = Val(a(2))
        c = Color.FromArgb(r, g, b)
        Return c
    End Function
    Private Function SS(ByVal s As String, ByVal v As String) As String
        Dim x As String = s
        If x Is Nothing OrElse x = "" Then
            x = v
        End If
        Return x
    End Function
    Private Function SD(ByVal s As String, ByVal v As Double) As Double
        Dim x As Double = s
        If s Is Nothing OrElse s = "" OrElse s = "0" Then
            x = v
        End If
        Return x
    End Function
    Private Function SI(ByVal s As String, ByVal v As Integer) As Integer
        Dim x As Integer = s
        If s Is Nothing OrElse s = "" Then
            x = v
        End If
        Return x
    End Function
    Private Function SB(ByVal s As String, ByVal v As Boolean) As Boolean
        Dim x As Boolean
        If s Is Nothing OrElse s = "" Then
            x = v
        ElseIf UCase(s) = "TRUE" Then
            x = True
        Else
            x = False
        End If
        Return x
    End Function
#End Region
#Region "Save and load methods"
    Public Sub SavePreferences(ByVal p_prefs As RCMS_Preferences, Optional ByVal p_path As String = "")
        Dim v_path As String
        v_path = p_path
        If v_path = "" Then
            v_path = p_prefs.g_file
        End If
        If v_path = "" Then
            v_path = Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Name) & "\RCMS_Prefs.xml"
            p_prefs.g_file = v_path
        End If
        Me.v_g_clds = p_prefs.g_clds
        Me.v_b_wp = p_prefs.b_wp
        Me.v_l_wp = p_prefs.l_wp
        Me.v_c_wp = C2S(p_prefs.c_wp)
        Me.v_f_wp = p_prefs.f_wp
        Me.v_b_zone = p_prefs.b_zone
        Me.v_l_zone = p_prefs.l_zone
        Me.v_c_zone = C2S(p_prefs.c_zone)
        Me.v_f_zone = p_prefs.f_zone
        Me.v_t_zone = p_prefs.t_zone
        Me.v_l_grid = p_prefs.l_grid
        Me.v_w_grid = p_prefs.w_grid
        Me.v_h_grid = p_prefs.h_grid
        Me.v_c_grid = p_prefs.c_grid
        Me.v_m_grid = p_prefs.m_grid
        Me.v_v_grid = p_prefs.v_grid
        Me.v_t_grid = p_prefs.t_grid
        Me.v_x_timo = p_prefs.x_timo
        Me.v_t_wpid = p_prefs.t_wpid
        Me.v_t_flor = p_prefs.t_flor
        Me.v_t_room = p_prefs.t_room
        Me.v_t_fpno = p_prefs.t_fpno
        Me.v_t_fpid = p_prefs.t_fpid
        Me.v_t_cbid = p_prefs.t_cbid
        Me.v_x_cbno = p_prefs.x_cbno
        Me.v_p_excl = p_prefs.p_excl
        Me.v_c_texc = C2S(p_prefs.c_texc)
        Me.v_c_hexc = C2S(p_prefs.c_hexc)
        If File.Exists(v_path) Then
            Dim d As String = "yyyyMMdd_HHmmss"
            Dim n As DateTime = DateTime.Now
            d = n.ToString(d)
            Dim b As String = v_path
            b = Path.GetFileNameWithoutExtension(b)
            b = b & "_" & d & ".xml"
            My.Computer.FileSystem.RenameFile(v_path, b)
        End If
        Dim objSerial As XmlSerializer
        Dim objStream As FileStream
        objStream = New FileStream(v_path, FileMode.OpenOrCreate)
        objSerial = New XmlSerializer(Me.GetType)
        objSerial.Serialize(objStream, Me)
        objStream.Close()
    End Sub
    Public Sub LoadPreferences(ByVal p_prefs As RCMS_Preferences, Optional ByVal p_path As String = "")
        Dim v_path As String
        v_path = p_path
        If v_path = "" Then
            v_path = CLDS_Static_Utils.CLDS_FindFile("RCMS_Prefs.xml")
        End If
        Dim objSerial As XmlSerializer
        Dim objStream As FileStream
        Dim x As Boolean = Microsoft.VisualBasic.FileIO.FileSystem.FileExists(v_path)
        p_prefs.g_file = v_path
        If Not x Then
            MsgBox("Unable to load preferences from XML file" & vbLf & v_path, MsgBoxStyle.Critical, "RCMS")
            Exit Sub
        End If
        objStream = New FileStream(v_path, FileMode.Open)
        objSerial = New XmlSerializer(Me.GetType)
        Dim p_loaded As Object = objSerial.Deserialize(objStream)
        objStream.Close()
        p_prefs.g_clds = SS(p_loaded.v_g_clds, d.g_clds)
        p_prefs.b_wp = SS(p_loaded.v_b_wp, d.b_wp)
        p_prefs.l_wp = SS(p_loaded.v_l_wp, d.l_wp)
        p_prefs.c_wp = SC(p_loaded.v_c_wp, d.c_wp)
        p_prefs.f_wp = SI(p_loaded.v_f_wp, d.f_wp)
        p_prefs.b_zone = SS(p_loaded.v_b_zone, d.b_zone)
        p_prefs.l_zone = SS(p_loaded.v_l_zone, d.l_zone)
        p_prefs.c_zone = SC(p_loaded.v_c_zone, d.c_zone)
        p_prefs.f_zone = SI(p_loaded.v_f_zone, d.f_zone)
        p_prefs.t_zone = SS(p_loaded.v_t_zone, d.t_zone)
        p_prefs.l_grid = SS(p_loaded.v_l_grid, d.l_grid)
        p_prefs.w_grid = SD(p_loaded.v_w_grid, d.w_grid)
        p_prefs.h_grid = SD(p_loaded.v_h_grid, d.h_grid)
        p_prefs.c_grid = SD(p_loaded.v_c_grid, d.c_grid)
        p_prefs.m_grid = SD(p_loaded.v_m_grid, d.m_grid)
        p_prefs.v_grid = SD(p_loaded.v_v_grid, d.v_grid)
        p_prefs.t_grid = SD(p_loaded.v_t_grid, d.t_grid)
        p_prefs.x_timo = SI(p_loaded.v_x_timo, d.x_timo)
        p_prefs.t_wpid = SS(p_loaded.v_t_wpid, d.t_wpid)
        p_prefs.t_flor = SS(p_loaded.v_t_flor, d.t_flor)
        p_prefs.t_room = SS(p_loaded.v_t_room, d.t_room)
        p_prefs.t_fpno = SS(p_loaded.v_t_fpno, d.t_fpno)
        p_prefs.t_fpid = SS(p_loaded.v_t_fpid, d.t_fpid)
        p_prefs.t_cbid = SS(p_loaded.v_t_cbid, d.t_cbid)
        p_prefs.x_cbno = SI(p_loaded.v_x_cbno, d.x_cbno)
        p_prefs.p_excl = SS(p_loaded.v_p_excl, d.p_excl)
        p_prefs.c_texc = SC(p_loaded.v_c_texc, d.c_texc)
        p_prefs.c_hexc = SC(p_loaded.v_c_hexc, d.c_hexc)
    End Sub
#End Region
End Class
